<?php  
class ControllerRestaurantBooking extends Controller {
	public function index() {
		// Check if this user is not logged in then redirect him to login page
		if (!$this->customer->isLogged()) {
	  		//$this->session->data['redirect'] = $this->url->link('account/account', '', 'SSL');
	  
			// Added strpos check to pass McAfee PCI compliance test (http://forum.opencart.com/viewtopic.php?f=10&t=12043&p=151494#p151295)
			if (isset($this->request->post['redirect']) && (strpos($this->request->post['redirect'], HTTP_SERVER) !== false || strpos($this->request->post['redirect'], HTTPS_SERVER) !== false)) {
				$redirect = $this->request->post['redirect'];
			} elseif (isset($this->request->get['redirect'])) {
				$redirect = urldecode(str_replace('&amp;', '&', $this->request->get['redirect']));
			} else {
				$redirect = '';
			}
			
			if(isset($this->request->get['layout']) && $this->request->get['layout'] == 'popup'){
		  		$this->redirect($this->url->link('account/login/popup', 'redirect=' . urlencode($redirect), 'SSL'));
			}else{
				$this->redirect($this->url->link('error/permission_denied/popup'));
			}
    	}
		
		$this->language->load('restaurant/booking');

		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_restaurant_info'] = $this->language->get('text_restaurant_info');
		$this->data['text_restaurant_name'] = $this->language->get('text_restaurant_name');
		$this->data['text_address'] = $this->language->get('text_address');
		$this->data['text_url'] = $this->language->get('text_url');
		$this->data['text_phone'] = $this->language->get('text_phone');
		$this->data['text_email'] = $this->language->get('text_email');
		$this->data['text_booking_info'] = $this->language->get('text_booking_info');
		$this->data['text_address'] = $this->language->get('text_address');
		$this->data['text_wait'] = $this->language->get('text_wait');
		
		$this->data['entry_num_of_people'] = $this->language->get('entry_num_of_people');
		$this->data['entry_date_booked'] = $this->language->get('entry_date_booked');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_telephone'] = $this->language->get('entry_telephone');
		$this->data['entry_request_detail'] = $this->language->get('entry_request_detail');
		$this->data['entry_captcha'] = $this->language->get('entry_captcha');
		
		$this->data['button_booking'] = $this->language->get('button_booking');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		
    	$this->data['action'] = $this->url->link('restaurant/booking', '', 'SSL');
    	
    	if(isset($this->request->get['restaurant_id'])){
    		$this->load->model('restaurant/restaurant');
    		$restaurant_info = $this->model_restaurant_restaurant->getRestaurant($this->request->get['restaurant_id']);
    	}
	
    	if ($restaurant_info) {
    		$this->data['restaurant_info'] = $restaurant_info;
			
			if ($this->config->get('config_booking_id')) {
				$this->load->model('content/information');
				
				$information_info = $this->model_content_information->getInformation($this->config->get('config_booking_id'));
				
				if ($information_info) {
					$this->data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information/info', 'information_id=' . $this->config->get('config_booking_id'), 'SSL'), $information_info['title'], $information_info['title']);
				} else {
					$this->data['text_agree'] = '';
				}
			} else {
				$this->data['text_agree'] = '';
			}
		
			if (isset($this->request->post['agree'])) {
	      		$this->data['agree'] = $this->request->post['agree'];
			} else {
				$this->data['agree'] = false;
			}
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/restaurant/booking.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/restaurant/booking.tpl';
			} else {
				$this->template = 'default/template/restaurant/booking.tpl';
			}
			
			$this->children = array(
			);
    	}else{
      		$this->document->setTitle($this->language->get('text_error'));

      		$this->data['heading_title'] = $this->language->get('text_error');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_close'] = $this->language->get('button_close');

      		$this->data['continue'] = $this->url->link('common/home');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/popup_not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/popup_not_found.tpl';
			} else {
				$this->template = 'default/template/error/popup_not_found.tpl';
			}
			
			$this->children = array(
			);
			
			$this->response->setOutput($this->render());
    	}
		
		$this->response->setOutput($this->render());
	}
	
	public function book(){
		$this->language->load('restaurant/booking');
		
		$json = array();
		
		// Check if this customer is logged or not
		if (!$this->customer->isLogged()) {
			$json['error'] = $this->language->get('error_permission');
		} else {
			$today = date("Y-m-d");
			$todays_date = strtotime($today);
			
			$date_info = date_parse_from_format('d-m-Y H:i', $this->request->post['date_booked']);
			$date_str = $date_info['year'] . '-' . $date_info['month'] . '-' . $date_info['day'];
			$booking_date = strtotime($date_str);
			
			if ((int)($this->request->post['num_of_people']) == 0) {
				$json['error'] = $this->language->get('error_num_of_people');
			}elseif ((utf8_strlen($this->request->post['date_booked']) == 0) || $booking_date < $todays_date) {
				$json['error'] = $this->language->get('error_date_booked');
			}elseif ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
				$json['error'] = $this->language->get('error_email');
			}elseif ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
	      		$json['error'] = $this->language->get('error_telephone');
			}elseif ((utf8_strlen($this->request->post['request_detail']) > 255)) {
	      		$json['error'] = $this->language->get('error_request_detail');
			}elseif (!$this->request->post['agree']) {
				if ($this->config->get('config_booking_id')) {
					$this->load->model('content/information');
					
					$information_info = $this->model_content_information->getInformation($this->config->get('config_booking_id'));
					
					if ($information_info && !$this->request->post['agree']) {
		      			$json['error'] = sprintf($this->language->get('error_agree'), $information_info['title']);
					}
				}
			}elseif (!isset($this->session->data['booking_captcha']) || ($this->session->data['booking_captcha'] != $this->request->post['captcha'])) {
				$json['error'] = $this->language->get('error_captcha');
			}
				
			if (($this->request->server['REQUEST_METHOD'] == 'POST') && !isset($json['error'])) {
				$this->load->model('restaurant/booking');
				
				$data = $this->request->post;
				$data['ip'] = $this->request->server['REMOTE_ADDR'];
				$data['booking_status_id'] = $this->config->get('config_booking_status_id');
				
				$this->model_restaurant_booking->addBooking($this->request->get['restaurant_id'], $data);
				
				$json['success'] = $this->language->get('text_success');
			}
		}
			
		$this->response->setOutput(json_encode($json));
	}

	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['booking_captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
}
?>